
import { res_format } from './res_format';
import sharp from 'sharp';
import fs from 'fs';

export namespace ImageCheck {
    export class ImageCheck {
        constructor(entery: string) {
            this.curRoot = entery;
            console.log('collect resource');
            let res = new res_format.ResCheck(this.curRoot).BuildTree();
            let imgPac: string[] = [];

            console.log('collect img');
            this.CollectImg(res.CurRoot, imgPac);

            console.log('start check');
            this.CheckImg(imgPac);
        }

        private CollectImg(root: res_format.ResFolder, pac: string[]) {
            for (let folder of root.FoldersName) {
                this.CollectImg(root.GetFolderByName(folder)!, pac);
            }

            let path: string;
            if (root.FilesNum > 0) {
                for (let file of root.Files!) {
                    path = `${this.curRoot}/${root.FullPath}/${file}`;
                    if (/\w*\.png$|\w*\.jpg$/.test(path)) {
                        pac.push(`/${root.FullPath}/${file}`);
                    }
                }
            }

        }


        private async CheckImg(imgPac: string[]): Promise<void> {

            let metaData: sharp.Metadata;

            let overSize: string[] = [];
            let unPlatte: string[] = []
            for (let path of imgPac) {
                console.log(`checking data, path: ${path}`);
                metaData = await sharp(`${this.curRoot}${path}`).metadata();
                if ((metaData?.width || 0) > 2048 || (metaData?.height || 0) > 2048) {
                    overSize.push(`${path}, size: ${metaData.width} by ${metaData.height}`);
                }
                // @ts-ignore
                if (metaData.format == 'png' && !metaData['paletteBitDepth']) { //没导出…… metadata.cc #173 info.Set("paletteBitDepth", baton->paletteBitDepth);
                    unPlatte.push(`${path}`);
                }
            }

            let reportPath = `${this.curRoot}/imgcheckReport.json`;
        
            let data = {
                oversize:
                {
                    num: overSize.length,
                    data: overSize
                },
                unplatte: {
                    num: unPlatte.length,
                    data: unPlatte
                }
            }

            let fscontext = fs.openSync(reportPath, 'w');

            fs.writeFileSync(reportPath, JSON.stringify(data, null, 4), { encoding: 'utf8' });

            fs.closeSync(fscontext);
			console.log(`gen report to ${reportPath}`);
            // console.log('#######################size thing########################');
            // for (let content of overSize) {
            //     console.log(content);
            // }

            // console.log('#####################palette thing########################');
            // for (let content of unPlate) {
            //     console.log(content);
            // }
        }

        private curRoot: string;
    }
}